<template>
<div class="about">
    <div class="site-content">
        <div class="content-warp">
            <div class="login">
                <el-form ref="loginForm" :model="loginForm" style="width:50%">
                    <h3 class="title">博客后台管理系统</h3>
                    <el-form-item prop="username">
                        <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
                        </el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码" @keyup.enter.native="handleLogin">
                        </el-input>
                    </el-form-item>
                    <el-form-item prop="captcha">
                        <el-input v-model="loginForm.captcha" auto-complete="off" placeholder="验证码" style="width: 63%">
                        </el-input>
                        <div class="login-code">
                            <el-tooltip class="item" effect="dark" content="点击刷新" placement="top-start">
                                <img :src="codeUrl" @click="getCode" />
                            </el-tooltip>
                        </div>
                    </el-form-item>

                    <el-form-item style="width:100%;">
                        <el-button round @click="otherWayVisible = true">其他方式登录</el-button>
                    </el-form-item>
                    <el-form-item style="width:100%;">
                        <el-button :loading="loading" size="medium" type="primary" style="width:100%;" @click.native.prevent="handleLogin">
                            <span v-if="!loading">登 录</span>
                            <span v-else>登 录 中...</span>
                        </el-button>
                    </el-form-item>
                </el-form>
            </div>

            <el-dialog title="其他方式" :visible.sync="otherWayVisible" style="width:100%;" center>
              <el-row>
                        <el-tooltip class="item" effect="dark" content="阴回调地址尚未在工信部备案，暂停使用" placement="top-start">
                            <el-button round @click="doQQLogin()" disabled>QQ登录</el-button>
                        </el-tooltip>
                   
                        <el-tooltip class="item" effect="dark" content="仅用于评论博客使用" placement="top-start">
                            <el-button round @click="doLogin('gitee')">Gitee登录</el-button>
                        </el-tooltip>
                   
                        <el-tooltip class="item" effect="dark" content="仅用于评论博客使用" placement="top-start">
                            <el-button round @click="doLogin('oschina')">开源中国登录</el-button>
                        </el-tooltip>
                   
                        <el-button round @click="registerFormVisible = true;otherWayVisible = false">注册</el-button>
                  
                </el-row>
                <div slot="footer" class="dialog-footer">
                </div>
            </el-dialog>
            <el-dialog title="注册" :visible.sync="registerFormVisible">
                <el-form :model="registerForm">
                    <el-form-item label="账号">
                        <el-input v-model="registerForm.username" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="密码">
                        <el-input v-model="registerForm.password" autocomplete="off" show-password></el-input>
                    </el-form-item>
                    <el-form-item label="确认密码">
                        <el-input v-model="registerForm.conFirmpassword" autocomplete="off" show-password></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button type="primary" @click="registerUser()">注册</el-button>
                </div>
            </el-dialog>
        </div>
    </div>
</div>
</template>

<script>
import {
    login,
    getCodeImg,
    getOauthLoginInfo
} from "@/api/index";
import {
    getToken
} from "@/utils/auth.js";

export default {
    name: "Login",
    data() {
        return {
            codeUrl: "",
            loginForm: {
                username: "zsw",
                password: "123456",
                captcha: "",
                state: "",
            },
            registerForm: {
                username: "",
                password: "",
                conFirmpassword: "",
            },
            loading: false,
            redirect: undefined,
            timer: null, // 定时器名称
            otherWayVisible: false,
            registerFormVisible: false,
        };
    },

    created() {
        this.getCode();
    },
    mounted() {
        var avatar = this.$store.getters.avatar;
        if (avatar) {
            this.$router.push("/");
        }
    },

    methods: {
        handleLogin() {
            this.loading = true;
            this.$store
                .dispatch("user/login", this.loginForm)
                .then(() => {
                    this.loading = false;
                    location.reload();
                })
                .catch(() => {
                    this.loading = false;
                });
        },
        getCode() {
            getCodeImg().then((res) => {
                // this.codeUrl = "data:image/gif;base64," + res.data.captcha;
                this.codeUrl =  res.data.captcha;
                this.loginForm.state = res.data.state;
            });
        },
        doQQLogin() {
            //获取QQ连接
            getOauthLoginInfo("qq").then((res) => {
                window.location.href = res.data;
            });
        },
        doGiteeLogin() {
            //获取gitee连接
            getOauthLoginInfo("gitee").then((res) => {
                window.location.href = res.data;
            });
        },
        doLogin(platform) {
            //获取gitee连接
            getOauthLoginInfo(platform).then((res) => {
                window.location.href = res.data;
            });
        },
        registerUser() {
            if (this.registerForm.password != this.registerForm.conFirmpassword) {
                this.$message.error("两次密码不一致");
                return;
            }
            this.$message.error("暂时不支持注册");
        },
    },
};
</script>

<style lang="scss">
.login {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 400px;
    margin-top: 100px;
    background-size: cover;
}

.title {
    margin: 0px auto 30px auto;
    text-align: center;
    color: #707070;
}

.login-form {
    border-radius: 6px;
    background: #ffffff;
    width: 400px;
    padding: 25px 25px 5px 25px;

    .el-input {
        height: 38px;

        input {
            height: 38px;
        }
    }

    .input-icon {
        height: 39px;
        width: 14px;
        margin-left: 2px;
    }
}

.login-tip {
    font-size: 13px;
    text-align: center;
    color: #bfbfbf;
}

.login-code {
    width: 30%;
    height: 38px;
    float: right;

    img {
        cursor: pointer;
        vertical-align: middle;
    }
}

.el-login-footer {
    height: 40px;
    line-height: 40px;
    position: fixed;
    bottom: 0;
    width: 100%;
    text-align: center;
    color: #fff;
    font-family: Arial;
    font-size: 12px;
    letter-spacing: 1px;
}
</style>
